package com.propertyportal.manageproperty;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import com.actionbarsherlock.app.SherlockFragment;
import com.propertyportal.alladapter.SectionListAdapter;
import com.propertyportal.manageagent.AddPerson;
import com.propertyportal.manageagent.ManageAgent;
import com.propertyportal.propertylistfragment.PropertyDetail;
import com.propertyportal.propertylistfragment.PropertyList;

public class SlidingMenuFragment extends SherlockFragment implements
		ExpandableListView.OnChildClickListener {
	private ExpandableListView expand;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		List<MenuSection> sectionList = createMenu();
		View view = inflater.inflate(R.layout.slidingmenu_fragment, container,
				false);
		this.expand = (ExpandableListView) view
				.findViewById(R.id.slidingmenu_view);
		this.expand.setGroupIndicator(null);

		SectionListAdapter sectionListAdapter = new SectionListAdapter(
				this.getActivity(), sectionList);
		
		this.expand.setAdapter(sectionListAdapter);

		this.expand
				.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
					@Override
					public boolean onGroupClick(ExpandableListView parent,
							View v, int groupPosition, long id) {
						return true;
					}
				});

		this.expand.setOnChildClickListener(this);

		int count = sectionListAdapter.getGroupCount();
		
		for (int position = 0; position < count; position++) {
			this.expand.expandGroup(position);
		}

		return view;
	};

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
	}

	private List<MenuSection> createMenu() {
		List<MenuSection> sectionList = new ArrayList<MenuSection>();

		MenuSection oDemoSection = new MenuSection("Options");
		oDemoSection.addSection(101, "Manage Property", "property");
		oDemoSection.addSection(102, "Manage Agent", "agent");
		oDemoSection.addSection(103, "View Report", "report");
		sectionList.add(oDemoSection);
		return sectionList;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		switch ((int) id) {
		case 101:
			// TODO	
			((ManageProperty) getActivity()).getSlidingMenu().toggle();
			if(!ManageProperty.looker){
			PropertyList pl = new PropertyList();
			getFragmentManager().beginTransaction().replace(R.id.headlines_fragment, pl).commit();

			PropertyDetail pd=new PropertyDetail();
	    	getFragmentManager().beginTransaction().replace(R.id.article_fragment, pd).commit();
			}
			else{
				PropertyList pl = new PropertyList();
				getFragmentManager().beginTransaction().replace(R.id.fragment1, pl).commit();
	
			}
			
			break;

		case 102:
			// TODO
			((ManageProperty) getActivity()).getSlidingMenu().toggle();
			if(!ManageProperty.looker){
			
			ManageAgent man = new ManageAgent();
			getFragmentManager().beginTransaction()
					.replace(R.id.article_fragment, man).commit();

			PropertyList pls = new PropertyList();
			getFragmentManager().beginTransaction()
					.replace(R.id.headlines_fragment, pls).commit();
			 }
			else{
				AddPerson agent = new AddPerson();
				getFragmentManager().beginTransaction().replace(R.id.fragment1, agent).commit();
				//Toast.makeText(getActivity(), "Agent",Toast.LENGTH_LONG).show();
			}

			break;

		case 201:
			// TODO

			break;
		case 202:
			// TODO

			break;
		}

		return false;
	}
}
